package com.yr.ryjk.consult;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yr.ryjk.common.ErrorType;
import com.yr.ryjk.consult.entity.Consult;
import com.yr.ryjk.consult.service.IConsultService;
import com.yr.ryjk.interceptor.authority.For;
import com.yr.ryjk.interceptor.authority.UserType;
import com.yr.ryjk.util.JSONUtil;
/**
 * 咨询
 * @author Luning
 *
 */
@For(user=UserType.APP_USER)
@Controller
@RequestMapping("/consult")
public class ConsultController {
	@Autowired
	private IConsultService consultService;
	
	/**
	 * 咨询信息入库
	 * @param phone
	 * @param doctorId
	 * @param title
	 * @param askContent
	 * @param picUrls 允许为空
	 * @return
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> insert(@RequestParam("phone") @NotBlank String phone,
			@RequestParam("doctorId") @NotBlank String doctorId,
			@RequestParam("title") @NotBlank String title,
			@RequestParam("askContent") @NotBlank String askContent,
			@RequestParam("patientName") @NotBlank String patientName,
			@RequestParam("patientGender") @NotBlank int patientGender,
			@RequestParam("patientAge") @NotBlank int patientAge,
			String picUrls) throws IllegalAccessException, InvocationTargetException{
		Consult consult = new Consult();
		consult.setDocId(doctorId);
		consult.setAskContent(askContent);
		consult.setTitle(title);
		consult.setPicUrls(picUrls);
		consult.setPatientName(patientName);
		consult.setPatientAge(patientAge);
		consult.setPatientGender(patientGender);
		
		return consultService.addConsult(consult,phone);
	}
		
	/**
	 * app医生在问答库中获取咨询内容列表信息
	 * @param docId
	 * @return
	 */
	@RequestMapping(value="/getContentList",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getContentList(@RequestParam(value="pageNo",defaultValue="1") int pageNo,
			@RequestParam(value="pageSize",defaultValue="10") int pageSize,
			@RequestParam("id") @NotBlank String id){
		return JSONUtil.getJSON(ErrorType.SUCCESS,
				consultService.getContentList(pageNo,pageSize,id));
	}
	
	/**
	 * app回复咨询接口
	 * @param conId 咨询id
	 * @param type 0 为用户，1 为医生
	 * @param isPush 是否推送,0不推送,非0推送(为实现app咨询中的通知功能)
	 * @param answerContent 内容
	 * @param picUrls 图片
	 * @return
	 */
	@RequestMapping(value="/reply",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addReply(@RequestParam("id") @NotBlank String id, 
			@RequestParam @NotNull Integer type, 
			@RequestParam(value="isPush",defaultValue="0") int isPush,
			String answerContent,String picUrls){
		return consultService.addReply(id,type,isPush,answerContent,picUrls);
	}
	
	/**
	 * app页面用户分页获取个人咨询记录接口
	 * @param pageNo
	 * @param pageSize
	 * @param status
	 * @param token 令牌，用来确定用户
	 * @return
	 */
	@RequestMapping(value="/user/list",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getUserList(@RequestParam(value="pageNo",defaultValue="1") int pageNo,
			@RequestParam(value="pageSize",defaultValue="10") int pageSize,
			@RequestParam(value="status",defaultValue="0") int status,
			@RequestParam @NotBlank String token){
		return JSONUtil.getJSON(ErrorType.SUCCESS,consultService.getListOfUser(pageNo,pageSize,status,token));
	}
	
	/**
	 * app用户获取未评价咨询记录
	 * @param pageNo
	 * @param pageSize
	 * @param token
	 * @return
	 */
	@For(user=UserType.APP_USER)
	@RequestMapping(value="/user/getUnScoreList",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getUnScoreList(@RequestParam(value="pageNo",defaultValue="1") int pageNo,
			@RequestParam(value="pageSize",defaultValue="10") int pageSize,
			@RequestParam @NotBlank String token){
		return JSONUtil.getJSON(ErrorType.SUCCESS,consultService.getUnScoreList(pageNo,pageSize,token));
	}
	
	/**
	 * app页面医生分页获取数据接口
	 * @param pageNo
	 * @param pageSize
	 * @param status
	 * @param token 令牌，用来确定用户
	 * @return
	 */
	@RequestMapping(value="/doctor/list",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getDoctorList(@RequestParam(value="pageNo",defaultValue="1") int pageNo,
			@RequestParam(value="pageSize",defaultValue="10") int pageSize,
			@RequestParam(value="status",defaultValue="0") int status,
			@RequestParam @NotBlank String token){
		return JSONUtil.getJSON(ErrorType.SUCCESS,consultService.getListOfDoctor(pageNo,pageSize,status,token));
	}
	
	/**
	 * 获取医生和病人之间的之前的咨询记录
	 * @param pageNo
	 * @param pageSize
	 * @param consultId 咨询id,用于确认用户和医生
	 * @return
	 */
	@RequestMapping(value="/listHistory",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> listHistory(@RequestParam(value="pageNo",defaultValue="1") int pageNo,
			@RequestParam(value="pageSize",defaultValue="10") int pageSize,
			@RequestParam("consultId") @NotBlank String consultId){
		return JSONUtil.getJSON(ErrorType.SUCCESS,consultService.getListHistory(pageNo,pageSize,consultId));
	}
	
	/**
	 * app结束本次咨询
	 * @param id
	 * @param type 3或7
	 * @return
	 */
	@RequestMapping(value="/end",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> end(@RequestParam("id") @NotBlank String id,
			@RequestParam("type") @NotBlank String type){
		consultService.updateEnd(id,type);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	
	/**
	 * 医生请求结束咨询
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/askForEnd",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> askForEnd(@RequestParam(value="id") String id){
		consultService.askForEnd(id);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	
	/**
	 * 用户拒绝结束咨询
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/refuseEnd",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> refuseEnd(@RequestParam(value="id") String id){
		consultService.refuseEnd(id);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	
	/**
	 * 用户删除咨询记录
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> delete(@RequestParam(value="consultId") String consultId){
		consultService.deleteConsultById(consultId);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	
	/**
	 * 用户获取未处理的咨询数量
	 * @return
	 */
	@RequestMapping(value="/getUnHandled",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getUnHandled(@RequestParam(value="token") @NotBlank String token){
		return JSONUtil.getJSON(ErrorType.SUCCESS,consultService.getUnHandledCount(token));
	}
}